package com.matete.agentmanage.service;



import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.matete.agentmanage.model.Time;
import com.matete.agentmanage.model.User;

public class TimeService {
	private static final Time timeDao = new Time().dao();
	private static final User userDao = new User().dao();
	
	/**
	 * 获取用户的开始时间结束时间，并在对应的RoomID下改变状态。
	 * @param ID
	 */
	public void changeState(String ID) {
		User user = userDao.findById(ID); //获取当前申请人
		Timestamp startTime = (Timestamp) user.getStartTime();
		Timestamp endTime = (Timestamp) user.getEndTime();
		Integer roomID = user.getRoomID();//从1计数
        TimeService timeService = new TimeService();

        // 获取时间段
        List<TimeList> times = null;
        try {
            times = timeService.getTimeLists();
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String nowDate = sdf.format(date);

		// 进行循环验证 每个时间段与开始时间结束时间进行验证
        int index = 0;
		for(TimeList time : times){
			Timestamp startTimePara = time.getStartTime(); // 时间段 开始
			Timestamp endTimePara = time.getEndTime();      // 时间段 结束
			/*System.out.println(startTimePara.after(startTime));
			System.out.println(endTimePara.before(endTime));
			System.out.println((startTimePara.before(startTime) && endTimePara.after(startTime)) || (startTimePara.before(endTime) && endTimePara.after(endTime))
					|| (startTimePara.after(startTime) && endTimePara.before(endTime)));
			System.out.println();*/
			if((startTimePara.before(startTime) && endTimePara.after(startTime)) || (startTimePara.before(endTime) && endTimePara.after(endTime))
					|| (startTimePara.after(startTime) && endTimePara.before(endTime)) || startTimePara.equals(startTime)) {  // 判断该开始时间与结束时间是否在该时间段内
                //System.out.println(1);
                String sql = "UPDATE time SET time_" + index +" =1 WHERE ID = ?";
                Db.update(sql, roomID); //将time表被申请的更新。
            }
		index ++;
		}
		
	}

    /**
     * 增加房间后也在time表生成房间
     */
	public void addRoomToTime() {
		Time time = new Time();
        time.save();
        return;
	}

    /**
     * 删除房间时，也从Time表中删除对应房间
     * @param id
     */
	public void delRoomeToTime(String id) {
        Time time = timeDao.findById(id);
        time.delete();
        return;
    }

	/**
	 * 将每天的时间存放在队列中，6：00 - 22：00
	 * @return
	 * @throws ParseException
	 */
	public List<TimeList> getTimeLists() throws ParseException {
        List<TimeList> timestampList = new ArrayList<TimeList>();
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String nowDate = sdf.format(date);
	    for (int i = 0; i < 17; i++) {
            SimpleDateFormat df1 = new SimpleDateFormat("HH:mm:ss");

            java.util.Date date1 = df1.parse((i + 6) + ":00:00");
            java.util.Date date2 = df1.parse((i + 7) + ":00:00");
            String time_1 = df1.format(date1);
            String time_2 = df1.format(date2);

            Timestamp startTime = Timestamp.valueOf(nowDate + " " + time_1);
            Timestamp endTime = Timestamp.valueOf(nowDate + " " + time_2);
            TimeList timeList = new TimeList(startTime, endTime);
            //System.out.println(timeList.getStartTime());

            timestampList.add(timeList);
        }
        return timestampList;
	}

    /**
     * 测试类
     * @param args
     */
    public static void main(String[] args) {
        TimeService timeService = new TimeService();
        try {
            List<TimeList> timestampList = timeService.getTimeLists();
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}


class TimeList{
	private Timestamp startTime;

	private Timestamp endTime;

	public TimeList(Timestamp startTime, Timestamp endTime) {
		this.startTime = startTime;
		this.endTime = endTime;
	}

	public Timestamp getStartTime() {
		return startTime;
	}

	public void setStartTime(Timestamp startTime) {
		this.startTime = startTime;
	}

	public Timestamp getEndTime() {
		return endTime;
	}

	public void setEndTime(Timestamp endTime) {
		this.endTime = endTime;
	}
}
